package hhm.android.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import hhm.android.library.bus.Bus;

public abstract class NewLazyFragment extends BaseFragment {

    private Context mContext;
    private boolean isFirstLoad = true; // 是否第一次加载

    private boolean isLIst = false; // 是否列表内部承载List 承载list则不需要OnResume刷新
    /**
     * 当外部是fragment hide+add show 展示 ，fragment内部存在viewpager2+fragment布局时，viewpager内部 fragment会一直保持在onResume状态，
     * 所以需要在内部存在viewpager的 外部fragment中处理网络请求的请求时机
     */
    private boolean hasOut = false; // 是否外部fragment
    private boolean outShow = true; // 外部fragment是否在显示中

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        Bus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(getContentViewId(), null);
        initView(view);
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isFirstLoad = true;
        Bus.getDefault().unregister(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirstLoad) {
            // 将数据加载逻辑放到onResume()方法中
            initData();
            initEvent();
            isFirstLoad = false;
        } else {
            if (!isLIst) {
                if (hasOut) {
                    if (outShow) {
                        if (isVisible()) {
                            getData();
                        }
                    }
                } else {
                    if (isVisible()) {
                        getData();
                    }
                }
            }
        }
    }

    /**
     * show hide 方法会走的回调方法 viewpager滑动不会走
     *
     * @param hidden
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            getData();
        }
    }

    /**
     * 设置布局资源id
     *
     * @return
     */
    protected abstract int getContentViewId();

    /**
     * 初始化视图
     *
     * @param view
     */
    protected void initView(View view) {

    }

    /**
     * 初始化数据
     */
    protected void initData() {

    }

    /**
     * 初始化事件
     */
    protected void initEvent() {

    }

    /**
     * 初始化事件
     */
    protected void getData() {

    }

    public int dp2px(Float dpValue) {
        return (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, getResources().getDisplayMetrics()));
    }

    public boolean isHasOut() {
        return hasOut;
    }

    public void setHasOut(boolean hasOut) {
        this.hasOut = hasOut;
    }

    public boolean isOutShow() {
        return outShow;
    }

    public void setOutShow(boolean outShow) {
        this.outShow = outShow;
    }

    public boolean isLIst() {
        return isLIst;
    }

    public void setLIst(boolean LIst) {
        isLIst = LIst;
    }

    public void showLoadingFragment(int viewId) {
        LoadingFragment mLoadingFragment = LoadingFragment.newInstance();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(viewId, mLoadingFragment, LoadingFragment.TAG)
                .commitAllowingStateLoss();
    }

    public void removeLoadingFragment() {
        LoadingFragment mLoadingFragment = findLoadingFragment();
        if (mLoadingFragment != null) {
            mLoadingFragment.removeSelf(getActivity().getSupportFragmentManager());
        }
    }

    public void showNoDataView() {
        LoadingFragment mLoadingFragment = findLoadingFragment();
        if (mLoadingFragment != null) {
            mLoadingFragment.showNoDataView();
        }

    }

    public LoadingFragment findLoadingFragment() {
        Fragment fragment = getActivity().getSupportFragmentManager().findFragmentByTag(LoadingFragment.TAG);
        if (fragment != null) {
            return (LoadingFragment) fragment;
        }
        return null;
    }
}